package com.tycoin.utils;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


public class DesMgr {

    private static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }

    private static byte[] hexStr2ByteArr(String strIn)
            throws Exception {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;

        byte[] arrOut = new byte[iLen / 2];
        for (int i = 0; i < iLen; i = i + 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
        }
        return arrOut;
    }

    public static String encrypt(String source, String key) throws Exception {
        //Security.addProvider(new com.sun.crypto.provider.SunJCE());
        String Algorithm = null;
        if (key.length() == 16) {
            Algorithm = "DES";
        } else if (key.length() == 48) {
            Algorithm = "DESede";
        }else{
            throw new Exception("key size error!");
        }
        String result = null;
        try {
            SecretKey deskey = new SecretKeySpec(hexStr2ByteArr(key), Algorithm);
            //加密
            javax.crypto.Cipher c1 = javax.crypto.Cipher.getInstance(Algorithm);
            c1.init(javax.crypto.Cipher.ENCRYPT_MODE, deskey);
            byte[] cipherByte = c1.doFinal(source.getBytes());
            result = byte2hex(cipherByte);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return result;
    }

    public static String decrypt(String source, String key) throws Exception {
        //Security.addProvider(new com.sun.crypto.provider.SunJCE());
        String Algorithm = null;
        if (key.length() == 16) {
            Algorithm = "DES";
        } else if (key.length() == 48) {
            Algorithm = "DESede";
        }else{
            throw new Exception("key size error!");
        }

        String result = null;
        try {
            SecretKey deskey = new SecretKeySpec(hexStr2ByteArr(key), Algorithm);
            javax.crypto.Cipher c1 = javax.crypto.Cipher.getInstance(Algorithm);
            c1.init(javax.crypto.Cipher.DECRYPT_MODE, deskey);
            byte[] cipherByte = c1.doFinal(hexStr2ByteArr(source));
            result = new String(cipherByte);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) throws Exception{
        //String rt = "C0D4B5E3BCC6CB0D90ABE08497407713";
        //String rt = "162829DD80BC9535A7013E5EA0F0F316";
        String key = "BFCDEF5D0D07E352";
        String src = "1312333333333333333333333333333333333333";
        String cryptText = DesMgr.encrypt(src,key);
        System.out.println("crypt="+cryptText);
        String plainText = DesMgr.decrypt(cryptText,key);
        System.out.println("plain="+plainText);
    }

}